//引入express
var express = require("express");
//引入路由
var apps = express.Router();
var UUID = require("uuid");
let db = require("../utils/db");

//c获取首页bannner
apps.get("/getbanner", function (req, res) {
    let sql = `SELECT * FROM banner LIMIT 3`;
    db.query(sql, (err, data) => {
        if (data) {
            console.log(data);
            res.json({
                code: 200,
                msq: "查询banner成功",
                data: data,
            });
        } else {
            res.json({
                code: 500,
                msq: "查询banner失败",
                err: err,
            });
        }
    });
});

//c获取热销
apps.post("/gethotsell", function (req, res) {
    let sql =
        "SELECT * FROM `shopping-mall`.`goods` ORDER BY `sall_num` DESC LIMIT 0,?";
    db.query(sql, [req.body.limit], (err, data) => {
        if (data) {
            console.log(data);
            res.json({
                code: 200,
                msq: "查询热销成功",
                data: data,
            });
        } else {
            res.json({
                code: 500,
                msq: "查询热销失败",
                err: err,
            });
        }
    });
});

//c获取综合排序
apps.get("/getcomp", function (req, res) {
    let sql =
        "SELECT * FROM `shopping-mall`.`goods` ORDER BY `stoke` DESC LIMIT 0,60";
    db.query(sql, (err, data) => {
        if (data) {
            console.log(data);
            res.json({
                code: 200,
                msq: "查询综合成功",
                data: data,
            });
        } else {
            res.json({
                code: 500,
                msq: "查询综合失败",
                err: err,
            });
        }
    });
});

//c获取一级分类
apps.get("/getgoods/classify", function (req, res) {
    let sql = "SELECT `id`,`name` FROM `goods_classify`";
    db.query(sql, (err, data) => {
        if (data) {
            console.log(data);
            res.json({
                code: 200,
                msq: "查询一级成功",
                data: data,
            });
        } else {
            res.json({
                code: 500,
                msq: "查询一级失败",
                err: err,
            });
        }
    });
});

//c获取价格高排序
apps.get("/getbigprice", function (req, res) {
    let sql =
        "SELECT * FROM `shopping-mall`.`goods` ORDER BY `sell_price` DESC LIMIT 0,60";
    db.query(sql, (err, data) => {
        if (data) {
            console.log(data);
            res.json({
                code: 200,
                msq: "查询价格排序成功",
                data: data,
            });
        } else {
            res.json({
                code: 500,
                msq: "查询价格排序失败",
                err: err,
            });
        }
    });
});

//c获取搜索记录
apps.get("/getsearch/history", function (req, res) {
    let sql = "SELECT * FROM search_history";
    db.query(sql, (err, data) => {
        if (data) {
            console.log(data);
            res.json({
                code: 200,
                msq: "查询搜索记录成功",
                data: data,
            });
        } else {
            res.json({
                code: 500,
                msq: "查询搜索记录失败",
                err: err,
            });
        }
    });
});

//c添加搜索记录
apps.post("/addsearch/history", function (req, res) {
    let sql =
        "INSERT INTO `shopping-mall`.`search_history` (`key_word`) VALUES (?)";
    db.query(sql, [req.body.key_word], (err, data) => {
        if (data) {
            console.log(data);
            res.json({
                code: 200,
                msq: "添加搜索记录成功",
                data: data,
            });
        } else {
            res.json({
                code: 500,
                msq: "添加搜索记录失败",
                err: err,
            });
        }
    });
});

//c删除搜索记录
apps.get("/delsearch/history", function (req, res) {
    let sql = "TRUNCATE TABLE search_history";
    db.query(sql, (err, data) => {
        if (data) {
            console.log(data);
            res.json({
                code: 200,
                msq: "删除搜索记录成功",
                data: data,
            });
        } else {
            res.json({
                code: 500,
                msq: "删除搜索记录失败",
                err: err,
            });
        }
    });
});

//c搜索
apps.post("/search", function (req, res) {
    let key_word = "%" + req.body.key_word + "%";
    let sql = "SELECT * FROM `goods` WHERE `name` LIKE ?";
    db.query(sql, [key_word], (err, data) => {
        if (data) {
            console.log(data);
            res.json({
                code: 200,
                msq: "搜索成功",
                key_word: req.body.key_word,
                data: data,
            });
        } else {
            res.json({
                code: 500,
                msq: "搜索失败",
                err: err,
            });
        }
    });
});

module.exports = apps;
